<?php

namespace app\service;

use MongoDB\Driver\Exception\BulkWriteException;
use support\Db;

class MongodbService
{

    public static ?MongodbService $_instance = null;
    protected                     $content;

    /**
     * @return MongodbService|null
     */
    public static function instance(): ?MongodbService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    public function getConnect($dbName = '')
    {
        if (empty($dbName)) $dbName = 'dian_cartoon';
        return Db::connection('mongodb')->collection($dbName);
    }

    public function initDb($dbName = ''): MongodbService
    {
        $this->content = $this->getConnect($dbName);
        return $this;
    }

    public function _find(array $where)
    {
        return $this->content->where($where)->first();
    }

    public function _insert(array $data = [])
    {
        return $this->content->insert($data);
    }

    public function _update(array $where = [], array $data = [])
    {
        return $this->content->where($where)->update($data);
    }

    public function _updateIn(array $where = [], array $data = [])
    {
        return $this->content->whereIn($where[0], $where[1])->update($data);
    }

    public function _select($where = [], $order = [], $page = 1, $limit = 10)
    {
        $page = $limit * (max(($page - 1), 0));
        return $this->content->where($where)->orderBy($order[0] ?? 'id', $order[1] ?? 'asc')->skip($page)->take($limit)->get()->toArray();
    }

    public function _count($where = [])
    {
        return $this->content->where($where)->count();
    }

    public function _selectIn(array $where = [], $order = [], $page = 1, $limit = 10)
    {
        $page = $limit * (max(($page - 1), 0));
        return $this->content->whereIn($where[0], $where[1])->orderBy($order[0] ?? 'id', $order[1] ?? 'asc')->skip($page)->take($limit)->get()->toArray();
    }

    public function _delete(array $where): bool
    {
        if (empty($where)) return false;
        return $this->content->where($where)->delete();
    }

    public function _converter_times(int $uid, int $vip): bool
    {
        try {
            $date            = date('Y-m-d');
            $where['uid']    = $uid;
            $where['date_v'] = $date;
            $where['vip']    = $vip;
            $has             = $this->initDb('converter_times')->_find($where);
            if ($has) {
                $this->content->where($where)->increment('times', 1);
            } else {
                $data = ['uid' => $uid, 'date_v' => $date, 'vip' => $vip, 'times' => 1];
                $this->_insert($data);
            }
            return true;
        } catch (BulkWriteException $exception) {
            return false;
        }
    }

    public function _converter_day_count(int $uid, string $date = '', bool $is_vip = false): int
    {
        if (empty($date)) $date = date('Y-m-d');
        try {
            $where['uid']    = $uid;
            $where['date_v'] = $date;
            $where['vip']    = !$is_vip ? 0 : 1;
            $has             = $this->initDb('converter_times')->_find($where);
            return $has['times'] ?? 0;
        } catch (BulkWriteException $exception) {
            return 0;
        }
    }
}
